// miniprogram/pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menus:[
      {
        id: 1,
        title: "砍价专区",
        desc: "0元观影",
        pic: ""
      },
      {
        id: 2,
        title: "领劵啦！",
        desc: "优惠购票",
        pic: ""
      },
      {
        id: 3,
        title: "在线观影",
        desc: "影片上线",
        pic: ""
      },
      {
        id: 4,
        title: "葛仙村",
        desc: "音乐盛宴",
        pic: ""
      },
      {
        id: 5,
        title: "怒火重案",
        desc: "得¥20",
        pic: ""
      },
      {
        id: 6,
        title: "看小说",
        desc: "重磅推荐",
        pic: ""
      },
      {
        id: 7,
        title: "猫眼商城",
        desc: "商品周边",
        pic: ""
      },
      {
        id: 8,
        title: "电影榜单",
        desc: "高分推荐",
        pic: ""
      },
    ]

  },
  _error(event) {
    console.log('父组件得到消息，子组件error点击了',event);
  },
  shopPopup() {
    console.log(this.popup);
    this.popup.showPopup();
  },
  openPosition(){

    wx.getLocation({
      type:"wgs84",
      success:(res)=>{
        this.setData({
          currentLon: res.longitude,
          currentLat: res.latitude,
        });
       wx.setStorageSync('currentLon', this.data.currentLon)
       wx.setStorageSync('currentLat', this.data.currentLat)
      },
      fail() {
        wx.getSetting({
          success:(res) =>{
            var statu = res.authSetting;
            console.log(statu);
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则定位功能将无法使用',
                success:(tip) => {
                  console.log(tip);
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          wx.getLocation({
                            success(res) {
                              this.setData({
                                currentLon: res.longitude,
                                currentLat: res.latitude,
                              });
                              wx.setStorageSync('currentLon', this.data.currentLon)
                              wx.setStorageSync('currentLat', this.data.currentLat)
                            },
                          });
                        } else {
                          wx.showToast({
                            title: '授权失败',
                            icon: 'error',
                            duration: 1000
                          })
                          wx.navigateBack({
                            delta: -1
                          });
                        }
                      }
                    })
                  }else{
                    wx.navigateBack({
                      delta: -1
                    });
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '调用授权窗口失败',
              icon: 'success',
              duration: 1000
            })
            wx.navigateBack({
              delta: -1
            });
          }
        })
      }
    
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    this.popup = this.selectComponent('#popup')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})